package com.wxxymakers.cklabrecruit.utils;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.stereotype.Component;

/**
 * @AUTHOR soft
 * @DATE 2018/11/17 20:46
 * @DESCRIBE 缓存工具
 */
@Component
public class CacheUtils {
    private final EhCacheManagerFactoryBean factoryBean;

    @Autowired
    public CacheUtils(EhCacheManagerFactoryBean factoryBean) {
        this.factoryBean = factoryBean;
    }

    public void put20(Object key, Object value) {
        int ttl = 20 * 60;
        Element element = new Element(key, value, false, ttl, ttl);
        getCache().put(element);
    }

    public Object get(String key) {
        Element element = getCache().get(key);
        if (element != null) {
            return element.getObjectValue();
        }
        return null;
    }

    private Ehcache getCache() {
        CacheManager object = factoryBean.getObject();
        return object.getEhcache("test");
    }
}
